package com.core.web.struts2.taglib;

import java.util.*;
import org.apache.struts2.views.jsp.*;

import javax.servlet.jsp.*;

import com.core.cache.*;
import com.core.util.*;

/**
 * text JSP 标签
 * 
 * 
 */
public class TextTag extends CommonTag {

	private String cachename = "";

	@Override
	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws javax.servlet.jsp.JspException {
		try {
			pageContext.getOut().print(getHtml());
		} catch (Exception e) {
			throw new JspException(e);
		}
		return EVAL_BODY_INCLUDE;
	}

	private String getHtml() {
		StringBuffer ret = new StringBuffer();
		Object stackObject = TagUtils.getStack(this.pageContext)
				.findValue(name);
		String inputValue = "";

		if (getValue() != null && !getValue().equals("")) {
			inputValue = getValue();
		}
		if (stackObject != null && !stackObject.equals("")) {
			inputValue = stackObject.toString();
		}

		if (this.format != null && !this.format.equals("")) {
			String type = "";
			String style = "";

			if (this.format.indexOf(":") == -1) {
				type = this.format;
			} else {
				type = format.substring(0, format.indexOf(":"));
				style = format.substring(format.indexOf(":") + 1, format
						.length());
			}

			try {
				if (type.equals("sub") && inputValue != null
						&& !inputValue.equals("")
						&& inputValue.length() > Integer.parseInt(style)) {// 值的字符串截取
					inputValue = inputValue.substring(0, Integer
							.parseInt(style));
				} else if (type.equals("num") && inputValue != null
						&& !inputValue.equals("")) {// 值的数字格式化处理
					inputValue = NumberUtil.formatNumber(inputValue, Integer
							.parseInt(style));
				} else if (type.equals("%") && inputValue != null
						&& !inputValue.equals("")) {// 值的格式百分比处理
					double temp = Double.parseDouble(NumberUtil.formatNumber(
							(new Double(inputValue).doubleValue() * 100) + "",
							Integer.parseInt(style)));
					if (temp == 0.00) {
						inputValue = "";
					} else {
						inputValue = (temp) + "%";
					}
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		/**
		 * 从通用缓存中取出相应值的显示信息
		 */
		if (this.cachename != null && !this.cachename.equals("")) {
			List<Common> datas = CommonCache.getInstance().getCommon(
					this.cachename);
			if (datas != null && !datas.isEmpty()) {
				for (Common data : datas) {
					if (data.getKey().equals(ret)) {
						inputValue = data.getValue();
						break;
					}
				}
			}
		}

		ret.append("<input type=\"text\" ").append(super.getCommonHtml())
				.append(" value=\"").append(inputValue).append("\" >");

		if (this.datatype != null && this.datatype.equals("Require")) {
			ret.append("<font color='red'>*</font>");
		}

		if (this.datatype != null
				&& (this.datatype.equals("Integer") || this.datatype
						.equals("Double"))
				&& (this.require == null || this.require.equals("true"))) {
			ret.append("<font color='red'>*</font>");
		}

		if (this.datatype != null
				&& (this.datatype.equals("Username") || this.datatype
						.equals("SafeString"))) {
			ret.append("<font color='red'>*</font>");
		}
		return ret.toString();
	}

	/**
	 * @return the cachename
	 */
	public String getCachename() {
		return cachename;
	}

	/**
	 * @param cachename
	 *            the cachename to set
	 */
	public void setCachename(String cachename) {
		this.cachename = cachename;
	}
}
